package com.vsoft.sms_cm;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.Logger;

import com.vsoft.framework.core.BaseException;
import com.vsoft.framework.core.BusiObj;
import com.vsoft.framework.core.db.OperAgent;
import com.vsoft.framework.core.db.SQLArgs;
import com.vsoft.framework.core.util.BOHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
//import .sms_onhook.TestHsqlDB;
public class TableCacheServerRedis {
	private static Logger vLogger = Logger.getLogger(TableCacheServerRedis.class);
	private static String vName = TableCacheServerRedis.class.getName();
    private static ConcurrentHashMap<String,ArrayList<BusiObj>> vData=null;
    private static ConcurrentHashMap<String,ArrayList<BusiObj>> vWhiteListData=null;
    private static ConcurrentHashMap<String,ArrayList<BusiObj>> vTempleGroupData=null;
    private final static String		RESULT			="RESULT";
	private String vDebugHome = "";

	Connection vconn	=	null;
	PreparedStatement pst = null;
	ResultSet rs = null;
	public static String genSqlTriggerPhone(String condition)
	{
		String sql 					=	" select distinct c.ent_code ent_code,a.order_sn sub_id,c.sms_temple_id sm_temple_id," +
											" c.sms_temple_context sm_temple_context, " +
											//modified at 20120219 如果是特服号码，则区号不取
											" decode(b.special_serv_flag,'1','',b.area_no) ||b.phone_num order_num," +
											" c.audit_flag audit_flag, c.cdr_type cdr_type ," +
											" a.alarm_num alarm_num,a.out_price_times out_price_times, " +
											" a.in_price_times in_price_times ,a.send_flag,"+//d.group_id group_id,d.group_type group_type , ,a.alarm_context alarm_context
											" nvl(a.true_VOUCH_NUMBER,a.VOUCH_NUMBER) true_VOUCH_NUMBER, a.special_serv_flag special_serv_flag" +//,a.physical_num physical_num
											//add at 20120212 增加计费号码、特服标志、物理号码（注释掉 20120219）三个字段
											" from order_info a,trigger_phone b,sms_templete c" +//,sms_temple_group d 
											" where b.sms_temple_id = c.sms_temple_id  " +
											" and a.ent_code=c.ent_code   " +
											" and a.state='1' and b.state='1' " +
											condition+
											"  " +//and d.sms_temple_id=c.sms_temple_id
											" order by c.sms_temple_id,a.order_sn desc ";//,d.group_type
		return sql;
	}
	private static String genTempleGroup(String condition)
	{
		String sql 					=	" select  t.sms_temple_id sms_temple_id,t.group_type group_type,t.group_id group_id ," +//客户群组
											" t.mobile_num mobile_num ,t.ent_code ent_code from (" +
											" select distinct d.sms_temple_id sms_temple_id,d.group_type group_type,d.group_id group_id ," +
											" a.mobile_num mobile_num ,a.ent_code ent_code" +
											" from customer a ,cust_group_member b,customer_group c,sms_temple_group d " +
											" where a.cust_code=b.cust_code and b.group_id=c.group_id " +
											" and c.group_id = d.group_id and  a.cust_type ='1' "+
											condition+
											" union " +//默认群组
											" select distinct c.sms_temple_id sms_temple_id,c.group_type group_type,c.group_id group_id, " +
											" 'A0' mobile_num,a.ent_code ent_code " +
											" from sms_templete a ,sms_temple_group c " +
											" where a.audit_flag='1' and a.state='1' " +
											" and a.sms_temple_id = c.sms_temple_id and c.group_type='0' " +
											condition+
											" )t " +
											" order by t.sms_temple_id,t.ent_code, t.group_type desc" +
											" ";
		return sql;
	}
	private static String genSqlWhite(String condition)
	{
		String sql 					=	" select  a1.ent_code ent_code,a1.mobile_num mobile_num" +
											" from  customer a1 " +
											" where a1.cust_type ='2' " +
											condition+
											" order by   a1.ent_code,a1.mobile_num desc" +
											"  ";
		return sql;
	}
	public static void  init(Connection conn) throws BaseException//BusiObj ddd
	{
		vLogger.info("\n====================init start====================\n");
		if(TableCacheServerRedis.checkIsInit())//已初始化，则清除缓存
		{
			vData.clear(); //可以用于刷新
			vWhiteListData.clear(); //可以用于刷新
			vTempleGroupData.clear(); //可以用于刷新
		}
		else//初始化缓存
		{
			vData=new ConcurrentHashMap<String,ArrayList<BusiObj>>();
			vWhiteListData=new ConcurrentHashMap<String,ArrayList<BusiObj>>();
			vTempleGroupData=new ConcurrentHashMap<String,ArrayList<BusiObj>>();
		}

		String sql 					=	TableCacheServerRedis.genSqlTriggerPhone("");
		SQLArgs 	Arg			= 	new SQLArgs();
		Arg.reset();
		vLogger.info("initialize cache (1)query smsTemple \n sql:" +sql);
		BusiObj		smsTempleBo 			= OperAgent.execQuery(conn, "getSubscriberAndSMSTemple", sql, Arg);
		//vLogger.info("返回数据是 initialize cache(1)query smsTemple:\n\n" + BOHelper.boToXML(smsTempleBo));
		//	 	GROUP_TYPE： 0：默认；1，客户群组；2：企业白名单
		Arg.reset();
		sql							=	TableCacheServerRedis.genTempleGroup("");

		vLogger.info("initialize cache (2)query smsTempleGroup \n sql:" +sql);
		BusiObj		templeGroupBo 			= OperAgent.execQuery(conn, "smsTempleGroup", sql, Arg);
		//vLogger.info("返回数据是 initialize cache(2)query smsTempleGroupAndWhite :\n\n" + BOHelper.boToXML(templeGroupBo));
		//白名单
 		sql							=	TableCacheServerRedis.genSqlWhite("");
 		Arg.reset();
		vLogger.info("initialize cache (3)query WhiteList \n sql:" +sql);
		BusiObj		whiteListBo 			= OperAgent.execQuery(conn, "WhiteList", sql, Arg);
		//vLogger.info("返回数据是 initialize cache (3)query WhiteList :\n\n" + BOHelper.boToXML(whiteListBo));
		//处理模板群组,key=sms_temple_id
		ArrayList<BusiObj> alTempleGroup				=	templeGroupBo.getBusiObjList(RESULT);
		String group_type ="",group_id="",sms_temple_id="",mobile_num="",ent_code="";
		for(BusiObj m:alTempleGroup)
		{
			group_type		=	m.getString("group_type");
			group_id			=	m.getString("group_id");
			sms_temple_id	=	m.getString("sms_temple_id");
			mobile_num		=	m.getString("mobile_num");//A0,默认群组
			ent_code			=	m.getString("ent_code");

			ArrayList<BusiObj> AlBo			=	vTempleGroupData.get(sms_temple_id);
			if(AlBo==null)
				AlBo										=	new	ArrayList<BusiObj>();
			AlBo.add(m);
			vTempleGroupData.put(sms_temple_id, AlBo);
		}
		vLogger.info("\n====================init put vTempleGroupData over====================\n");
		//处理白名单，key=手机号码，即主叫号码
		ArrayList<BusiObj> alWhite			=	whiteListBo.getBusiObjList(RESULT);
		for(BusiObj m:alWhite)
		{
			mobile_num			=	m.getString("mobile_num");
			ent_code				=	m.getString("ent_code");

			ArrayList<BusiObj> AlBo			=	vWhiteListData.get(ent_code);
			if(AlBo==null)
				AlBo										=	new	ArrayList<BusiObj>();
			AlBo.add(m);
			vWhiteListData.put(ent_code, AlBo);
		}
		vLogger.info("\n====================init put vWhiteListData over====================\n");
		//处理短信模板，key=被叫号码，即触发电话
		ArrayList<BusiObj> alSms				=	smsTempleBo.getBusiObjList(RESULT);
		String order_num	= "";
		for(BusiObj m:alSms)
		{
			order_num = m.getString("order_num");
			ArrayList<BusiObj> AlBo			=	vData.get(order_num);
			if(AlBo==null)
				AlBo										=	new	ArrayList<BusiObj>();
			//m	=	TableCacheServerRedis.formatAlarmContext(m);
			AlBo.add(m);
			vData.put(order_num, AlBo);
		}
		vLogger.info("\n====================init put vData over====================\n");
		vLogger.info("\n====================init end====================\n");

		// and c.audit_flag='1' and c.eff_date<=sysdate and c.exp_date>=sysdate
	}
	private static BusiObj formatAlarmContext(BusiObj m) throws BaseException
	{
		String alarm_context	=	m.getString("alarm_context","");
		String	in_price_times	=	m.getString("in_price_times","");
		if(alarm_context.trim().equals("")||in_price_times.trim().equals(""))
		{
			alarm_context="挂机短信本月发送已超过限制";
		}
		else
		{
			alarm_context=String.format(alarm_context,Integer.parseInt(in_price_times));
		}
		m.setString("alarm_context", alarm_context);
			
		return m;
	}
	private static  boolean checkIsInit()
	{
		if(vData==null||vTempleGroupData==null||vWhiteListData==null)
			return false;
		else
			return true;
		
	}
	private static  void  removeCacheByEntcode(BusiObj bo) throws BaseException
	{
		
		String ent_code			=	bo.getString("ent_code");
		Connection conn		=	bo.getConnection();
		String sql 					= 	" select a.sms_temple_id sms_temple_id ,b.area_no||b.phone_num order_num " +
											" from sms_templete a,trigger_phone b " +
											" where a.sms_temple_id=b.sms_temple_id and a.ent_code=:ent_code";
		SQLArgs 	Arg			= 	new SQLArgs();
		Arg.reset();
		Arg.setArg("ent_code", ent_code);
		vLogger.info("removeCacheByEntcode \n sql:" +sql);
		BusiObj		smsTempleBo 			= OperAgent.execQuery(conn, "removeCacheByEntcode", sql, Arg);
		vLogger.info("返回数据是 removeCacheByEntcode:\n\n" + BOHelper.boToXML(smsTempleBo));
		ArrayList<BusiObj> alTemple				=	smsTempleBo.getBusiObjList(RESULT);
		/////////////////////////////remove whiteList
		vWhiteListData.remove(ent_code);
		/////////////////////////////remove order_num and group
		HashMap <String,String> hOrder = new HashMap <String,String> ();
		HashMap <String,String> hTemple = new HashMap <String,String> ();
		for(BusiObj m:alTemple)
		{
			String 	sms_temple_id 	= m.getString("sms_temple_id");
			String	order_num			=	m.getString("order_num");
			String	sOrder				=	hOrder.get(order_num);
			if(sOrder==null)
			//hash中取不到值，即为null
			//如果hash中该key取值不为空，则说明已进行过remove操作，不需要再处理
			{
				vData.remove(order_num);
				hOrder.put(order_num, "1");
			}
			String	sTemple			=	hTemple.get(sms_temple_id);
			if(sTemple==null)
			//hash中取不到值，即为null
			//如果hash中该key取值不为空，则说明已进行过remove操作，不需要再处理
			{
				vTempleGroupData.remove(sms_temple_id);
				hTemple.put(sms_temple_id, "1");
			}
		}
		
	}
	//检查企业是否有效
	private static  boolean checkEntValid(BusiObj bo) throws BaseException
	{
		String ent_code			=	bo.getString("ent_code");
		Connection conn		=	bo.getConnection();
		String sql = " select count(1) from enterprise where state='1' and ent_code =:ent_code";
		SQLArgs 	Arg			= 	new SQLArgs();
		Arg.reset();
		Arg.setArg("ent_code", ent_code);
		vLogger.info("check enterprise is valid \n sql:" +sql);
		int cnt 			= OperAgent.QueryInt(conn, "checkEntValid", sql, Arg);
		vLogger.info("返回数据是 checkEntValid:\n\n" + cnt);
		
		if(cnt<1)
			return false;//无有效企业
		else
			return true;
		
	}
	
	public static String getOneStaticValue(BusiObj bo)throws BaseException
	{
		Connection conn 	=	bo.getConnection();
		String table_name=bo.getString("table_name");
		SQLArgs 	Arg			= 	new SQLArgs();
 		String sql							=	"select a.code from static_data a where upper(a.table_name )=:table_name";
 		Arg.reset();
		vLogger.info("query static data : \n sql:" +sql);
		Arg.setArg("table_name", table_name);
		BusiObj		staticBo 			= OperAgent.execQuery(conn, "query static data", sql, Arg);
		vLogger.info("返回数据是 query static data ::\n\n" + BOHelper.boToXML(staticBo));
		String rt			=	"";	
		ArrayList<BusiObj> Al_bos = staticBo.getBusiObjList(RESULT);
		//多余一条记录仅取第一条，否则返回空串
		if(Al_bos.size()>0)
		{
			BusiObj BO=Al_bos.get(0);
			rt=BO.getString("code");
		}

		return rt;
	}
	public static BusiObj getTempleForSend(BusiObj bo) throws BaseException
	{
		BusiObj retBo=new BusiObj();
		vLogger.info("\n====================getTempleForSend start====================\n");
		retBo	=TableCacheServerRedis.getTempleForSendIn(bo);
		vLogger.info( "getTempleForSend返回数据：\n"+BOHelper.boToXML(retBo));
		vLogger.info("\n====================getTempleForSend end====================\n");
		return retBo;
	}
	private static BusiObj getTempleForSendIn(BusiObj bo) throws BaseException
	{
		String desc ="";
		String order_num="";
		String mobile_num="";
		String cdr_type	="";
		String cdr_type_name="";
		String	ent_code	=	"",group_type="";
		String prompt="";
		BusiObj retBo=new BusiObj();
		try
		{
			Connection conn	=	bo.getConnection();
			if(!TableCacheServerRedis.checkIsInit())//未初始化
			{
				
				TableCacheServerRedis.init(conn);
				//throw new BaseException(vName +"-001","请初始化获取短信模板功能。");
			}
			String alarm_context ="";
			//获取alarm_context
			{
				BusiObj		StaticBO	=new BusiObj() ;
				StaticBO.setConnection(conn);
				StaticBO.setString("TABLE_NAME", "order_info.alarm_context".toUpperCase());
				alarm_context 	=	TableCacheServerRedis.getOneStaticValue(StaticBO);
			}
			
	
			order_num	=	bo.getString("order_num");
			mobile_num	=	bo.getString("mobile_num");
			cdr_type		=	bo.getString("cdr_type").trim();//
			cdr_type_name=TableCacheServerRedis.getCdrTypeName(cdr_type);
			
			prompt=	"======查询触发电话："+order_num+"\n 主叫号码："+mobile_num+"\n 通话状态："+cdr_type+"("+cdr_type_name+")"+"的短信模板======\n";
			vLogger.info(prompt);
			
			HashMap<String,String> hAddedTempleId= new HashMap<String,String>();
			HashMap<String,String> hWhiteMobileNum= new HashMap<String,String>();
			ArrayList<BusiObj> arWhiteList = null;
			ArrayList<BusiObj> arTempleGroup = null;
			String whiteTempleId="";
			//////////////////////////////////
			BusiObj logBo=new BusiObj();
			logBo.setConnection(conn);
			logBo.setString("order_num", order_num);
			logBo.setString("mobile_num", mobile_num);
			logBo.setString("cdr_type", cdr_type);

			//////////////////////////////////
			////////////////////////////////////开始业务处理
			ArrayList<BusiObj> arData = vData.get(order_num);//获取触发电话的缓存
			int	cnt=0;
			if (arData!=null)
				cnt=arData.size();
			int	i=0,whiteCnt=0,groupCnt=0,cdrTypeCnt=0,auditCnt=0;
			//1、该触发电话是否配置模板
			if(arData == null||cnt==0)
			{
				desc= "触发电话:"+order_num+";    没有配置短信模板。";
				retBo.setString("desc",desc);//
				retBo.setValue(RESULT,new ArrayList<BusiObj>());
				//vLogger.info( BOHelper.boToXML(retBo));
				return retBo;
			}
			//有模板
			else
			{
				for(BusiObj m :arData)//遍历触发电话模板和群组
				{
					vLogger.info("(1)===============触发电话:"+order_num+"的模板："+ BOHelper.boToXML(m));
					
					String templeCdrType=m.getString("cdr_type");
					String audit_flag=m.getString("audit_flag");
			//2、该模板是否审核，如果cnt》0，即模板数》0而审核数==0，则出现无审核模板信息
					if(!audit_flag.trim().equals("1"))//模板未审核
						continue;
					auditCnt++;//模板审核计数器+1
					//过滤cdr type,如果cdr_type不适配，则短信模板计数器不会增加,但是cdrtype的计数器会+1
					if(!(templeCdrType.trim().equals("4")||templeCdrType.trim().equals(cdr_type)))
						continue;
					cdrTypeCnt++;
			//3、cdr_type是否匹配，如果cnt》0，即模板数》0而cdr_type匹配数==0，则出现无匹配模板
					ent_code	=	m.getString("ent_code");
					//group_type= m.getString("group_type");
					String smsTempleId	=	m.getString("sm_temple_id");
					/////////////////////////处理白名单/////////////////////////////
					arWhiteList=vWhiteListData.get(ent_code);//加载企业的白名单缓存
					boolean	whiteFlag=false;//白名单标志 
					if(!(arWhiteList==null||arWhiteList.size()==0))//主叫存在白名单
					{
						for(BusiObj n :arWhiteList)
						{
							String white_mobile_num	=	n.getString("mobile_num");
							
							if(white_mobile_num==null ||(white_mobile_num.trim().equals("")))
							//白名单的企业不等于模板的企业，则为非白名单，跳过
							{
								continue;
							}
							vLogger.info("(2)===============号码"+white_mobile_num+"为企业"+ent_code+"白名单："+ BOHelper.boToXML(n));
							if(white_mobile_num.equals(mobile_num))
							//白名单的企业等于模板的企业，则为白名单，白名单计数+1
							{
								whiteTempleId = smsTempleId;
								hWhiteMobileNum.put(white_mobile_num, "0");//设置白名单手机号码
								whiteCnt++;//白名单总数,每个模板都会校验主叫是否为白名单，如果该触发电话有多个模板，而主叫为该企业的白名单，则白名单总数=模板总数，如果该触发电话属于多个企业，则会取出另一企业的模板
								whiteFlag	=	true;//设置白名单标志，只要这个电话是企业白名单，就跳过，白名单与模板无关
								vLogger.info("(2.0.99)===============主叫号码"+mobile_num+"为企业"+ent_code+"白名单：");
								break;//主叫号码是白名单，则换一个模板
	
							}
						}
					}
					if(whiteFlag)//只要这个电话是企业白名单，就跳过该模板，白名单与模板无关，不再查询群组
					{
						whiteFlag=false;//add at 20110721
						continue;
					}
					/////////////////////////处理白名单完毕/////////////////////////////
					//白名单与模板群组处理有关的是hWhiteEnt（白名单企业hash）和whiteCnt（白名单计数器）
					/////////////////////////处理短信模板群组/////////////////////////////
					arTempleGroup=vTempleGroupData.get(smsTempleId);
					if(arTempleGroup==null||arTempleGroup.size()==0)//该模板没有模板群组,则跳过本次模板
					{
						vLogger.info("(3.0.1)===============模板id="+smsTempleId+"没有发送群组。");
						continue;
					}
					for(BusiObj j :arTempleGroup)
					{
						//String groupTempleId	=	j.getString("sms_temple_id");
						group_type=j.getString("group_type");
						String white_mobile_num=hWhiteMobileNum.get(mobile_num);
						String mobile=j.getString("mobile_num");
						vLogger.info("(3)===============触发电话"+order_num+"；模板id"+smsTempleId+"的发送群组："+ BOHelper.boToXML(j));
						if((white_mobile_num==null||white_mobile_num.trim().equals("")))//如果主叫非该企业白名单
						//该企业白名单不包含该主叫号码,则加入该模板记录
						{
							//两种情况则可加入：1、群组是默认群组，不需要匹配主叫；2、群组为客户群组，需要匹配主叫
							if(group_type.trim().equals("0")||mobile.trim().equals(mobile_num))
							{
								groupCnt++;//返回记录数+1
								if(hAddedTempleId.get(smsTempleId)==null)//相同的模板只加一次，不分默认群组还是客户群组
								{
									BusiObj formatSms =new BusiObj();
									String in_price_times = m.getString("in_price_times");
									formatSms.setString("alarm_context", alarm_context);
									formatSms.setString("in_price_times", in_price_times);
									formatSms = TableCacheServerRedis.formatAlarmContext(formatSms);
									String sms_context = formatSms.getString("alarm_context");
									m.setString("alarm_context", sms_context);
									retBo.setValue(RESULT, m);
									hAddedTempleId.put(smsTempleId, smsTempleId);//要考虑主叫号码是白名单的情况
									vLogger.info("(4)===============模板id:"+smsTempleId+"已加入本次返回结果。groupCnt="+groupCnt);
								}
								else
								{
									vLogger.info("(10003)===============模板id:"+smsTempleId+"已加入本次返回结果，不再重复加入。groupCnt="+groupCnt);
								}
							}
							else
							{
								vLogger.info("(10002)===============group type(非默认群组):"+group_type+"群组成员电话:"+mobile+"；主叫电话"+mobile_num);
							}
						}
						else
						{
							vLogger.info("(10001)===============白名单电话:"+white_mobile_num);
						}
	
					}
					/////////////////////////处理短信模板群组完毕/////////////////////////////
				}
				if(auditCnt==0&&cnt>0)
				{
					desc	=	"触发电话："+order_num+"的短信模板未审核";
					retBo.setString("desc", desc);//，待审核的短信模板共"+auditCnt+"条。
					retBo.setValue(RESULT,new ArrayList<BusiObj>());
					//vLogger.info( BOHelper.boToXML(retBo));
					return retBo;
				}
				
				if(cdrTypeCnt==0&&cnt>0)
				{
	/*				HashMap<String,String> cdrType= new HashMap<String,String>();
					cdrType.put("1", "忙");
					cdrType.put("2", "未接");
					cdrType.put("3", "通话");*/
					desc ="没有符合触发电话："+order_num+"本次通话状态:"+cdr_type+"("+cdr_type_name+")"+"的短信模板。";
					retBo.setString("desc",desc );//
					retBo.setValue(RESULT,new ArrayList<BusiObj>());
					//vLogger.info( BOHelper.boToXML(retBo));
					return retBo;
				}
				if(whiteCnt==cnt)
				{
					desc ="主叫电话:"+mobile_num+";    为触发电话："+order_num+"的白名单，不需发送短信。";
					retBo.setString("desc", desc);//
					retBo.setValue(RESULT,new ArrayList<BusiObj>());
					//vLogger.info( BOHelper.boToXML(retBo));
					return retBo;
				}
				if(groupCnt==0)
				{
					desc ="触发电话："+order_num+"没有配置短信模板发送群组。";
					retBo.setString("desc",desc );//
					retBo.setValue(RESULT,new ArrayList<BusiObj>());
					//vLogger.info( BOHelper.boToXML(retBo));
					return retBo;
				}
			}
			vLogger.info("查询触发电话："+order_num+"\n 主叫号码："+mobile_num+"\n 通话状态："+cdr_type+"("+cdr_type_name+")"+"：\n" );
			return retBo;
		}
		catch(Throwable e)
		{
			desc		="查询触发电话："+order_num+"\n 主叫号码："+mobile_num+"\n 通话状态："+cdr_type+"("+cdr_type_name+")"+"的短信模板出现错误：\n"+e.getMessage();
			retBo.setString("desc",desc );//
			return retBo;
		}
		
	}
	public static String getCdrTypeName(String cdr_type)
	{
		String cdr_type_name="";
		if(cdr_type==null || cdr_type.trim().equals(""))
			cdr_type_name="未定义的cdr_type";
		if(cdr_type.equals("1"))
			cdr_type_name="通话";
		else if(cdr_type.equals("2"))
			cdr_type_name="占线";
		else if(cdr_type.equals("3"))
			cdr_type_name="无应答";
		else
			cdr_type_name="";
		return cdr_type_name;

	}
/*	public static void refresh (BusiObj bo) throws BaseException
	{
		Connection conn		=	bo.getConnection();
		if(!TableCacheServerRedis.checkIsInit())//未初始化
		{
			TableCacheServerRedis.init(conn);
			return;
		}
		TableCacheServerRedis.refreshCallNumber(bo);
		TableCacheServerRedis.refreshTriggerPhone(bo);
	}*/
	public static void refreshTriggerPhone (BusiObj bo) throws BaseException
	{
		Connection conn		=	bo.getConnection();
		if(!TableCacheServerRedis.checkIsInit())//未初始化
		{
			TableCacheServerRedis.init(conn);
			return;
		}
		//String order_num		=	bo.getString("order_num");
		String ent_code		=	bo.getString("ent_code");
		String condition			=	" and a.ent_code=  "+ent_code+"  ";//	" and b.area_no||b.phone_num=  "+order_num+"  "
		String sql					=	TableCacheServerRedis.genSqlTriggerPhone(condition);
		SQLArgs 	Arg			= 	new SQLArgs();
		Arg.reset();
		vLogger.info("refresh cache query smsTemple by triggerPhone\n sql:" +sql);
		BusiObj		smsTempleBo 			= OperAgent.execQuery(conn, "query smsTemple by triggerPhone", sql, Arg);
		vLogger.info("返回数据是 refresh cache(1)query query smsTemple by triggerPhone:\n\n" + BOHelper.boToXML(smsTempleBo));
		
		//处理短信模板，key=被叫号码，即触发电话
		ArrayList<BusiObj> alSms				=	smsTempleBo.getBusiObjList(RESULT);
		HashMap<String,String> hRemove	=	new HashMap<String,String>();
		ArrayList<BusiObj> AlBo	=null;
		for(BusiObj m:alSms)
		{
			String order_num = m.getString("order_num");
			if(vData.get(order_num)!=null&&(hRemove.get(order_num)==null))
			{
				vData.remove(order_num);
				AlBo=	new	ArrayList<BusiObj>();
				hRemove.put(order_num, "0");//means this trigger phone has removes
			}
			else if(vData.get(order_num)==null)
			{
				AlBo=	new	ArrayList<BusiObj>();
				hRemove.put(order_num, "0");//means this trigger phone has removes,so the trigger phone cannot be removed
			}
			else
			{
				AlBo	=	vData.get(order_num);
			}
			AlBo.add(m);
			vData.put(order_num, AlBo);
			vLogger.info("(1)===============刷新触发电话"+order_num+"的模板信息："+ BOHelper.boToXML(m));
		}

/*		ArrayList<BusiObj> arData = OperAgent.execQuery("根据号码获取数据");
		vData.put(num,arData);*/
	}
	public static void refreshCallNumber (BusiObj bo) throws BaseException
	{
		try
		{
			Connection conn		=	bo.getConnection();
			if(!TableCacheServerRedis.checkIsInit())//未初始化
			{
				TableCacheServerRedis.init(conn);
				return;
			}
			if(!TableCacheServerRedis.checkEntValid(bo))//无有效企业，则删除缓存
			{
				TableCacheServerRedis.removeCacheByEntcode(bo);
				return;
			}
			String ent_code			=	bo.getString("ent_code");
			vLogger.info( "刷新缓存,企业编码:"+ent_code+"，入参包为:\n\n"+ BOHelper.boToXML(bo)+"\n" +
								"==========refresh cache start===============");
			///////////因调用关系，共用一个接口，先刷触发电话
			TableCacheServerRedis.refreshTriggerPhone(bo);
			////////////////////
			
			String condition			=	" and a.ent_code=  "+ent_code+"  ";
	
			SQLArgs 	Arg			= 	new SQLArgs();
			//	 	GROUP_TYPE： 0：默认；1，客户群组；2：企业白名单
			Arg.reset();
			String	sql				=	TableCacheServerRedis.genTempleGroup(condition);
			vLogger.info("refresh cache  query smsTempleGroup \n sql:" +sql);
			BusiObj		templeGroupBo 			= OperAgent.execQuery(conn, "smsTempleGroup", sql, Arg);
			vLogger.info("返回数据是 refresh cache(2)query smsTempleGroup :\n\n" + BOHelper.boToXML(templeGroupBo));
			
			//处理模板群组,key=sms_temple_id
			ArrayList<BusiObj> alTempleGroup				=	templeGroupBo.getBusiObjList(RESULT);
			String group_type ="",group_id="",sms_temple_id="",mobile_num="";
			HashMap<String,String> h	=	new HashMap<String,String>();
			for(BusiObj m:alTempleGroup)
			{
				group_type		=	m.getString("group_type");
				group_id			=	m.getString("group_id");
				sms_temple_id	=	m.getString("sms_temple_id");
				mobile_num		=	m.getString("mobile_num");//A0,默认群组
				ent_code			=	m.getString("ent_code");
	
				ArrayList<BusiObj> AlBo			=	vTempleGroupData.get(sms_temple_id);
				if(AlBo==null)
				{
					AlBo										=	new	ArrayList<BusiObj>();
					h.put(sms_temple_id, "1");//表示缓存里面没有该模板，打入hash后，第二次进入的话，就不会删除缓存
				}
				else
				{
					String has_remove_temple	=	h.get(sms_temple_id);
					if(has_remove_temple==null||has_remove_temple.trim().equals(""))//只删除一次对应模板的缓存
					{
						h.put(sms_temple_id, "0");
						vTempleGroupData.remove(sms_temple_id);
					}
				}
				AlBo.add(m);
				vTempleGroupData.put(sms_temple_id, AlBo);
				vLogger.info("(2)===============刷新模板"+sms_temple_id+"的群组信息："+ BOHelper.boToXML(m));
			}
			//处理白名单，key=手机号码，即主叫号码
			//白名单
			 condition					=	" and a1.ent_code=  "+ent_code+"  ";
			sql							=	TableCacheServerRedis.genSqlWhite(condition);
			Arg.reset();
			vLogger.info("refresh cache query WhiteList \n sql:" +sql);
			BusiObj		whiteListBo 			= OperAgent.execQuery(conn, "refresh cache  query WhiteList", sql, Arg);
			vLogger.info("返回数据是 refresh cache  query WhiteList :\n\n" + BOHelper.boToXML(whiteListBo));
			ArrayList<BusiObj> alWhite			=	whiteListBo.getBusiObjList(RESULT);
			HashMap<String,String> hWhite	=	new HashMap<String,String>();
			for(BusiObj m:alWhite)
			{
				mobile_num			=	m.getString("mobile_num");
				ArrayList<BusiObj> AlBo			=	vWhiteListData.get(ent_code);
				if(AlBo==null)
				{
					AlBo										=	new	ArrayList<BusiObj>();
					hWhite.put(ent_code, "1");//表示缓存里面没有企业客户，打入hash后，第二次进入的话，就不会删除缓存
				}
				else
				{
					String has_remove_white	=	hWhite.get(ent_code);
	
					if(has_remove_white==null||has_remove_white.trim().equals(""))//只删除一次对应企业的缓存
					{
						hWhite.put(ent_code, "0");
						vWhiteListData.remove(ent_code);
					}
				}
				AlBo.add(m);
				vWhiteListData.put(ent_code, AlBo);
				vLogger.info("(2)===============刷新企业"+ent_code+"的白名单信息："+ BOHelper.boToXML(m));
			}
			vLogger.info( "刷新缓存,企业编码:"+ent_code+"\n" +
			"==========refresh cache end===============");
		}
		catch (Throwable e)
		{
			vLogger.info("刷新缓存失败，入参包为:\n\n"+ BOHelper.boToXML(bo));
		}
		
		
/*		//处理短信模板，key=被叫号码，即触发电话
		ArrayList<BusiObj> alSms				=	smsTempleBo.getBusiObjList(RESULT);
		if(vData.get(order_num)!=null)
			vData.remove(order_num);
		ArrayList<BusiObj> AlBo			=	new	ArrayList<BusiObj>();
		for(BusiObj m:alSms)
		{
			order_num = m.getString("order_num");
			AlBo.add(m);
		}
		vData.put(order_num, AlBo);*/
/*		ArrayList<BusiObj> arData = OperAgent.execQuery("根据号码获取数据");
		vData.put(num,arData);*/
	}


	
	public static void main(String[] args)  {
		
		try{
			//ETLLoger.getInstance().printInfo("---------------InitSystem-------------");
			int i=0;
			System.out.println("vName"+vName);
			vLogger = Logger.getLogger(TableCacheServerRedis.class);
			vLogger.debug(vName);
			vLogger.info("---------------InitSystem1-------------");
			

		}

		catch(Exception e)
		{
			vLogger.error(e.getCause());
			e.printStackTrace();
		}
		finally
		{
		/*	try{
				if(pst != null) pst.close();
				if(rs != null) rs.close();
			}*/
		}
			vLogger.info("---------------Game Over-------------");
	

		
	}
	

}
